<template>
  <div>
    {{ getRoleList() }} {{ getMenuList() }}
    <div class="table">
      <div class="table-head">

      </div>
      <div class="table-body">
        <el-table
          :data="roleList"
          style="width: 100%;margin-bottom: 20px;"
          :header-cell-style="{background:'#DCDCDC',color:'#606266'}">
          <el-table-column prop="id" label="编号" width="50" header-align="center" align="center" class="table-header">
          </el-table-column>
          <el-table-column prop="name" label="角色名称" width="80" header-align="center" align="center"
                           class="table-header">
          </el-table-column>
          <el-table-column prop="status" label="状态" width="80" header-align="center" align="center"
                           class="table-header">
          </el-table-column>
          <el-table-column prop="createdUser" label="创建者" width="80" header-align="center" align="center"
                           class="table-header">
          </el-table-column>
          <el-table-column prop="createdTime" label="创建时间" width="180" header-align="center" align="center"
                           class="table-header">
          </el-table-column>
          <el-table-column prop="modifiedUser" label="修改者" width="80" header-align="center" align="center"
                           class="table-header">
          </el-table-column>
          <el-table-column prop="modifiedTime" label="修改时间" width="180" header-align="center" align="center"
                           class="table-header">
          </el-table-column>
          <el-table-column label="操作" header-align="center" align="center">
            <template slot-scope="s">
              <el-button type="info" size="mini" @click="showMenus(s.$index,s.row)">查看权限</el-button>
              <el-button type="primary" size="mini" @click="toUpdateBaseInfo(s.$index,s.row)">编辑</el-button>
              <el-button type="danger" size="mini" @click="del(s.$index,s.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <div class="tree" style="OVERFLOW: auto">
      <div class="tree-head">
        <div class="tree-btn">
          <div class="addbtn">
            <el-button type="success" size="mini" @click="toAdd">添加角色</el-button>
          </div>
          <div class="updateMenusBtn">
            编辑当前角色权限
            <el-switch
              v-model="updateMenus"
              @change="toUpdateMenus"
              active-color="#13ce66"
              inactive-color="#ff4949">
            </el-switch>
          </div>
        </div>
      </div>
      <div class="tree-alert" id="alertDiv"></div>
      <div class="tree-body" ref="treebody">
        <div class="addDiv" ref="adddiv">
          <el-form :model="m" label-width="80px">
            <el-form-item label="角色名称:">
              <el-input size="mini" v-model="m.name"></el-input>
            </el-form-item>
            <el-form-item label="角色状态:">
              <el-radio-group v-model="m.status">
                <el-radio :label="1">已激活</el-radio>
                <el-radio :label="0">未激活</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-form>
        </div>
        <div class="treeOnlyBody">
          <el-tree
            ref="tree"
            :data="menuList"
            show-checkbox
            node-key="id"
            :default-expanded-keys="expandedArray"
            :props="dataProps">
          </el-tree>
        </div>
      </div>
      <div class="tree-buttom">
        <el-button type="warning" @click="ok">确定</el-button>
        <el-button @click="cancel">取消</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";
axios.defaults.withCredentials=true

export default {
  name: "RoleM",
  data() {
    return {
      menuChildIds:[],
      isAddOperation: false,
      updateMenus: false,
      expandedArray: [],
      refreshR: 0,
      refreshM: 0,
      m: {
        id: '',
        name: '',
        status: '',
        createdUser: '',
        createdTime: '',
        modifiedUser: '',
        modifiedTime: '',
        menuIds: []
      },
      roleList: [],
      menuList: [],
      dataProps: {
        label: 'name',
        children: 'children'
      }
    };
  },
  methods: {
    getRoleList: function () {
      if (this.refreshR === 0) {
        this.refreshR++;
        var that = this;
        axios.get("http://localhost:8081/role/all").then(function (result) {
          that.roleList = result.data.data;
        })
      }
    },
    getMenuList: function () {
      if (this.refreshM === 0) {
        this.refreshM++;
        var that = this;
        axios.get("http://localhost:8081/menu/all").then(function (result) {
          that.menuList = result.data.data.menus;
          /*that.expandedArray = result.data.data.menuIds;*/
        })
      }
    },


    showMenus: function (index, row) {
      this.$refs.tree.setCheckedKeys(row.menuLastChildIds);
      this.m = row;
    },
    toAdd: function () {
      this.m = {};
      this.$refs.adddiv.style.display = 'block';
      this.$refs.tree.setCheckedKeys([]);
      this.updateMenus = true;
      this.$refs.treebody.style.pointerEvents = 'auto';
      this.isAddOperation = true;
    },
    toUpdateBaseInfo: function (index, row) {
      this.showMenus(index, row);
      this.$refs.adddiv.style.display = 'block';
      this.updateMenus = true;
      this.$refs.treebody.style.pointerEvents = 'auto';
      this.isAddOperation = false;
    },
    toUpdateMenus: function () {
      if (this.updateMenus) {
        this.$refs.treebody.style.pointerEvents = 'auto';
      } else {
        this.$refs.treebody.style.pointerEvents = 'none';
      }
      this.isAddOperation = false;
    },

    del: function (index,row) {
      var that = this;
      axios.delete("http://localhost:8081/role/"+row.id).then(function (result){
        that.messageAlert(result);
        that.refreshR = 0;
        that.getRoleList();
      })
    },
    ok: function () {
      var that = this;
      if (this.isAddOperation) {
        this.m.menuIds = this.$refs.tree.getCheckedKeys().concat(this.$refs.tree.getHalfCheckedKeys());
        axios.post("http://localhost:8081/role", this.m).then(function (result) {
          that.messageAlert(result);
          that.refreshR = 0;
          that.getRoleList();
        })
      } else {
        this.m.menuIds = this.$refs.tree.getCheckedKeys().concat(this.$refs.tree.getHalfCheckedKeys());
        axios.put("http://localhost:8081/role", this.m).then(function (result) {
          that.messageAlert(result);
          that.refreshR = 0;
          that.getRoleList();
        })
      }
      this.cancel();


    },
    cancel: function () {
      this.$refs.adddiv.style.display = 'none';
      this.updateMenus = false;
      this.toUpdateMenus();
    },
    messageAlert: function (result) {
      if (result.data.status === 1) {
        this.$message({
          message: result.data.msg,
          center: true,
          type: "success"
        });
      } else {
        this.$message({
          message: result.data.msg,
          center: true,
          type: "error"
        });
      }
    }


  }
}
</script>

<style scoped>


.table {

  float: left;
  width: 60%;
  height: 800px;
}

.table-head {

  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  padding-top: 10px;
  padding-left: 10px;
  padding-bottom: 10px;
}

.table-body {
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  margin-top: 10px;
}

.tree {
  float: left;
  margin-left: 2%;
  width: 36%;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1)
}

.tree-head {
  margin-top: 10px;
}


.addbtn {
  float: left;
  margin-left: 20px;
}

.addDiv {
  width: 300px;
  display: none;
}

.updateMenusBtn {
  margin-left: 200px;
}

.tree-body {
  pointer-events: none;
  margin-top: 50px;
  margin-left: 100px;
}


.tree-buttom {
  margin-bottom: 30px;
  text-align: center;
}


</style>
